<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 栏目管理
 */
namespace app\ais\controller\info;
use app\ais\controller\Common;
use app\ais\model\AisInfoCate;
use app\ais\model\AisInfo;
use app\ais\model\AisInfoTpl;

class Cate extends Common{


    public function initialize() {
        parent::initialize();
        $this->assign('pathMaps',[['name'=>'信息分类','url'=>url("info.cate/index")]]);
    }

    /**
     * 列表
     */
    public function index(){
        $view['lists']   = AisInfoCate::where(['member_miniapp_id' => $this->member_miniapp_id])->order('sort desc,id desc')->paginate(20); 
        $view['tabs']    = [
            ['name' =>'主题管理','url' =>url('info.cate/index'),'action' => 1],
            ['name' =>'模板库','url' =>url('info.tpl/index')],
            ['name' =>'推广设置','url' =>url('setting/info')],
        ];
        return view()->assign($view);
    }


    //编辑
    public function edit(){
        if(request()->isAjax()){
            $data = [
                'id'        => $this->request->param('id/d',0),
                'title'     => $this->request->param('title/s'),
                'name'      => $this->request->param('name/s'),
                'sort'      => $this->request->param('sort/d',0),
                'tpl_id'    => $this->request->param('tpl_id/d'),
            ];
            $validate = $this->validate($data,'Category.edit');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            if($data['tpl_id']){
                $tpl = AisInfoTpl::where($this->mini_program)->where(['id'=>$data['tpl_id']])->field('fields')->find();
                $data['fields'] = empty($tpl)?'[]' : $tpl->fields;
            }else{
                $data['fields'] = '[]';
            }
            $result =  AisInfoCate::edit($data);
            if($result){
                return json(['code'=>200,'url'=>url('info.cate/index'),'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            $view['id']  = $this->request->param('id/d',0);
            $view['info']= AisInfoCate::where($this->mini_program)->where(['id' => $view['id']])->find();
            $view['tpl'] = AisInfoTpl::where($this->mini_program)->order('id asc')->select();
            return view('edit',$view);
        }
    }

    /**
     * 排序
     */
    public function sort(){
        if(request()->isAjax()){
            $data = [
                'sort' => $this->request->param('sort/d'),
                'id'   => $this->request->param('id/d'),
            ];
            $validate = $this->validate($data,'Cate.sort');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result = AisInfoCate::update(['sort'=>$data['sort'],'id' => $data['id']]);
            if($result){
                return enjson(200);
            }else{
                return enjson(0);
            }
        }
    }

    //删除
    public function delete(){
        $id = $this->request->param('id/d');
        $info = AisInfo::where(['cate_id' => $id])->count();
        if($info){
            return enjson(0,'删除失败,栏目中还包含内容');
        }
        $result = AisInfoCate::where(['id'=>$id,'member_miniapp_id' => $this->member_miniapp_id])->delete();
        if($result){
            return enjson(200);
        }else{
            return enjson(0);
        } 
    }
}